<?php
/** .-------------------------------------------------------------------
* |  Software: [HDPHP framework]
* |      Site: www.hdphp.com
* |-------------------------------------------------------------------
* |    Author: 向军 <2300071698@qq.com>
* |    WeChat: aihoudun
* | Copyright (c) 2012-2019, www.houdunwang.com. All Rights Reserved.
* '-------------------------------------------------------------------*/

namespace system\model;

use hdphp\Model\Model;

class Goods extends Model{
	//数据表
	protected $table = "goods";

	//允许填充字段
	protected $allowFill = [ ];

	//禁止填充字段
	protected $denyFill = [ ];

	//完整表名
	protected $full = false;

	//自动验证
	protected $validate=[
		//['字段名','验证方法','提示信息',验证条件,验证时间]
        ['gname','required','商品名不能为空',self::MUST_VALIDATE,self::MODEL_BOTH],
        ['gnumber','required','商品货号不能为空',self::MUST_VALIDATE,self::MODEL_BOTH],
        ['unit','required','请填写计件单位',self::MUST_VALIDATE,self::MODEL_BOTH],
        ['marketprice','num:1,999999999','请填写1-999999999之间的数',self::MUST_VALIDATE,self::MODEL_BOTH],
        ['shopprice','num:1,99999999','请填写1-999999999之间的数',self::MUST_VALIDATE,self::MODEL_BOTH],
        ['pic','required','请添加图片',self::MUST_VALIDATE,self::MODEL_BOTH],
        ['total','num:1,999999999','请填写1-999999999之间的数',self::MUST_VALIDATE,self::MODEL_BOTH],
        ['bid','required','请填写所属品牌',self::MUST_VALIDATE,self::MODEL_BOTH],
        ['cid','required','请填写所属分类',self::MUST_VALIDATE,self::MODEL_BOTH],

	];

	//自动完成
	protected $auto=[
		//['字段名','处理方法','方法类型',验证条件,验证时机]
        ['time','time','function',self::MUST_AUTO,self::MODEL_INSERT]
	];

	//自动过滤
    protected $filter=[
        //[表单字段名,过滤条件,处理时间]


    ];

	//时间操作,需要表中存在created_at,updated_at字段
	protected $timestamps=false;

    public function store(){
        //1.商品表的添加
        $Goods = new Goods();
        $Goods['gname'] = Q('post.gname');
        $Goods['gnumber'] = Q('post.gnumber');
        $Goods['unit'] = Q('post.unit');
        $Goods['marketprice'] = Q('post.marketprice');
        $Goods['shopprice'] = Q('post.shopprice');
        $Goods['pic'] = Q('post.pic');
        $Goods['click'] = Q('post.click');
        $Goods['total'] = Q('post.total');
        $Goods['aid'] = $_SESSION['admin']['aid'];
        $Goods['bid'] = Q('post.bid');
        $Goods['cid'] = Q('post.cid');
        $Goods['tid'] = Q('post.tid');
        $gid = $Goods->save();
        //2.商品详情表的添加
        $logo = Q('post.logo');
        if(!isset($logo)){
            $this->error = '请填加商品图册';
            return false;
        }
        //处理缩略图
        $small = '';$medium = '';$big = '';
        foreach($logo as $k=>$v){
            $small .= Image::thumb($v, dirname($v) . '/small_' . basename($v), 50, 50, 5) . ',';
            $medium .= Image::thumb($v, dirname($v) . '/medium_' . basename($v), 400, 400, 5) . ',';
            $big .= Image::thumb($v, dirname($v) . '/big_' . basename($v), 800, 800, 5) . ',';
        }
        //去字符串最后的逗号
        $small = rtrim($small,',');
        $medium = rtrim($medium,',');
        $big = rtrim($big,',');
        //执行添加
        $detail = new \system\model\Detail();
        $detail['small'] = $small;
        $detail['medium'] = $medium;
        $detail['big'] = $big;
        $detail['intro'] = Q('post.intro');
        $detail['service'] = Q('post.service');
        $detail['gid'] = $gid;
        $detail->save();
        //3.商品属性表的添加
        $goodsAttr = new \system\model\GoodsAttr();
        //商品属性
        $attr = Q('post.attr');
        if(!isset($attr)){
            $this->error = '请填加商品属性';
            return false;
        }
        foreach($attr as $k=>$v){
            if($v){
                $goodsAttr['gtvalue'] = $v;
                $goodsAttr['added'] = 0;
                $goodsAttr['taid'] = $k;
                $goodsAttr['gid'] = $gid;
                $goodsAttr->save();
            }
        }
        //商品规格
        $spec = Q('post.spec');
        if(!isset($spec)){
            $this->error = '请填加商品规格';return false;
        }
        foreach($spec as $k=>$v){
            foreach($v['color'] as $kk=>$vv){
                $goodsAttr['gtvalue'] = $vv;
                $goodsAttr['added'] = $v['added'][$kk];
                $goodsAttr['taid'] = $k;
                $goodsAttr['gid'] = $gid;
                $goodsAttr->save();
            }
        }
        return true;
    }

    //编辑
    public function edit(){
        //获取get参数
        $gid=Q('post.gid');
        $Goods=Goods::find($gid);
        //编辑商品名
        $Goods['gname'] = Q('post.gname');
        //编辑商品货号
        $Goods['gnumber'] = Q('post.gnumber');
        //编辑计件单位
        $Goods['unit'] = Q('post.unit');
        //编辑市场价
        $Goods['marketprice'] = Q('post.marketprice');
        //编辑商城价
        $Goods['shopprice'] = Q('post.shopprice');
        //编辑图片
        $Goods['pic'] = Q('post.pic');
        //编辑点击次数
        $Goods['click'] = Q('post.click');
        //编辑库存
        $Goods['total'] = Q('post.total');
        //编辑用户
        $Goods['aid'] = $_SESSION['admin']['aid'];
        //编辑品牌
        $Goods['bid'] = Q('post.bid');
        //编辑分类
        $Goods['cid'] = Q('post.cid');
        //编辑类型
        $Goods['tid'] = Q('post.tid');
        $Goods->save();
        //实例化goodsAttr模型
        $goodsAttr = new \system\model\GoodsAttr();
        //先删后添
        $goodsAttr->where('gid',$gid)->delete();
        //商品属性
        $attr = Q('post.attr');
        if(!isset($attr)){
            $this->error = '请填加商品属性';
            return false;
        }
        foreach($attr as $k=>$v){
            if($v){
                $goodsAttr['gtvalue'] = $v;
                $goodsAttr['added'] = 0;
                $goodsAttr['taid'] = $k;
                $goodsAttr['gid'] = $gid;
                $goodsAttr->save();
            }
        };
        //商品规格
        $spec = Q('post.spec');
        if(!isset($spec)){
            $this->error = '请填加商品规格';return false;
        }
        foreach($spec as $k=>$v){
            foreach($v['color'] as $kk=>$vv){
                $goodsAttr['gtvalue'] = $vv;
                $goodsAttr['added'] = $v['added'][$kk];
                $goodsAttr['taid'] = $k;
                $goodsAttr['gid'] = $gid;
                $goodsAttr->save();
            }
        };

        $logo = Q('post.logo');
//		p(substr(basename("attachment/2016/11/09/medium_16981478669866.jpg"),0,6));die;
        foreach($logo as $k=>$v){
            //当旧图片还是旧数据的时候
            if(substr(basename($v),0,6)=='medium'){
                $logo[$k] = dirname($v) . '/' . ltrim(basename($v),'medium_');
            }
        }
        //处理缩略图
        $small = '';$medium = '';$big = '';
        foreach($logo as $k=>$v){
            $fname=str_replace('medium','',basename($v));
            $small .= Image::thumb($v, dirname($v) . '/small_' . $fname, 50, 50, 5) . ',';
            $medium .= Image::thumb($v, dirname($v) . '/medium_' . $fname, 400, 400, 5) . ',';
            $big .= Image::thumb($v, dirname($v) . '/big_' . $fname, 800, 800, 5) . ',';
        }
        //去字符串最后的逗号
        $small = rtrim($small,',');
        $medium = rtrim($medium,',');
        $big = rtrim($big,',');
        $data = [
            'small'=>$small,
            'medium'=>$medium,
            'big'=>$big,
            'intro'=>Q('post.intro'),
            'service'=>Q('post.service')
        ];
        $detail = new \system\model\Detail();
        $detail->where('gid',$gid)->update($data);

        return true;
    }




}